package org.osomit.sacct.entity;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;

import java.util.List;

/**
 *
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 28, 2009
 *
 */
public class Response extends Entity {
    private static final long serialVersionUID = 2397043725428012057L;
    public final static String ENTITY_NAME = "Response";

    //random string padding to the response
    public static final String RND = "rnd";
    private String rnd;

    public static final String COMMAND = "command";
    private String command;

    public final static String IS_SUCCESSFUL = "isSuccessful";
	private boolean isSuccessful;

    public final static String ERROR_CODE = "errorCode";
    private String errorCode;

	public final static String RETURN_VALUE = "returnValue";
	protected List returnValue;

    public String getRnd() {
        return rnd;
    }

    public void setRnd(String rnd) {
        this.rnd = rnd;
    }

    public String getCommand() {
        return command;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public boolean isSuccessful() {
        return isSuccessful;
    }

    public void setSuccessful(boolean successful) {
        isSuccessful = successful;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public List getReturnValue() {
        return returnValue;
    }

    public void setReturnValue(List returnValue) {
        this.returnValue = returnValue;
    }

    public JSONObject toJSON() {
        JSONObject obj = new JSONObject();
        obj.put(RND, this.rnd);
        obj.put(COMMAND, this.command);
        obj.put(IS_SUCCESSFUL, this.isSuccessful);
        JSONArray arr = new JSONArray();
        if(this.returnValue != null && this.returnValue.size() > 0){
            for(Object v: this.returnValue){
                if(v instanceof Entity){
                    arr.add(((Entity)v).toJSON());
                }else{
                    arr.add(v);
                }
            }
        }
        obj.put(RETURN_VALUE, arr);
        obj.put(ERROR_CODE, this.errorCode);

        return obj;
    }
}
